import {ActionsOptionItem, Props} from '@/components/typing';
import { useForm } from '@ant-design-vue/use';
import { clearForm, cloneForm } from '@/utils/form-util';
import {reactive} from "vue";
import moment from 'moment'
// FormHook
export const useFormHook = (formInfo: Props, rules: object): Props => {
  // 使用form的hook函数
  const { resetFields, validate, validateInfos, initialModel } = useForm(formInfo, rules);

  // 新增
  const add = (field?: string): void => {
    resetFields();
    clearForm(formInfo);
    cloneForm(formInfo, initialModel, field);
  };

  // 编辑
  const update = (info: Props, field?: string): void => {
    resetFields();
    cloneForm(formInfo, info, field);
  };
  const updateNew = (info: Props): void => {
    resetFields();
    if (info){
      Object.keys(info).forEach(key => {
        formInfo[key] = info[key];
      });
    }
  };

  // 验证Form
  const validateForm = async () => {
    return await validate();
  };

  return {
    validateInfos,
    add,
    update,
    updateNew,
    validateForm,
    resetFields
  }
}